/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.opentoast.maven;

import java.io.File;
import java.io.IOException;
import java.util.Set;

import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.util.FileUtils;

/**
 * Builds SpringSource dm Server par package
 * 
 * @author <a href="mailto:jiwhiz@gmail.com">Yuan Ji</a>
 *
 * @goal dm-par
 * @phase package
 * @requiresDependencyResolution test
 */
public class ParMojo extends AbstractS2DMMojo
{
    /**
     * Directory that resources are copied to during the build.
     *
     * @parameter expression="${project.build.directory}/${project.build.finalName}"
     * @required
     */
    private File workDirectory;


    /**
     * The directory for the generated PAR.
     *
     * @parameter expression="${project.build.directory}"
     * @required
     */
    private String outputDirectory;

    /**
     * The name of the PAR file to generate.
     *
     * @parameter alias="parName" expression="${project.build.finalName}"
     * @required
     */
    private String finalName;

    /**
     * The Jar archiver.
     *
     * @parameter expression="${component.org.codehaus.plexus.archiver.Archiver#jar}"
     * @required
     */
    private JarArchiver jarArchiver;

    /**
     * The maven archiver to use.
     *
     * @parameter
     */
    private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();

    /**
     * package dependencies and manifest into PAR
     */
    @SuppressWarnings("unchecked")
    public void execute()
        throws MojoExecutionException, MojoFailureException
    {
        getLog().debug( "S2DM Plugin ParMojo --> Copy dependencies ..." );
        Set<Artifact> deps = (Set<Artifact>)project.getDependencyArtifacts();
        
        try {
            for (Artifact dependency : deps){
                final File sourceFile = dependency.getFile();
                final File destinationFile = new File(workDirectory, sourceFile.getName());
                FileUtils.copyFile( sourceFile, destinationFile );
            }

        } catch (IOException e) {
            throw new MojoExecutionException( "Error copying dependency", e );
        }
        
        getLog().debug( "S2DM Plugin ParMojo --> generate manifest ..." );
        ParManifestWriter writer = new ParManifestWriter();
        writer.init(getLog(), project, workDirectory);
        writer.write();
        
        getLog().debug( "S2DM Plugin ParMojo --> package par ..." );
        try
        {
            File parFile = new File( outputDirectory, finalName + ".par" );
            MavenArchiver archiver = new MavenArchiver();
            getLog().debug( "Jar archiver implementation[" + jarArchiver.getClass().getName() + "]" );
            archiver.setArchiver( jarArchiver );
            archiver.setOutputFile( parFile );
            archive.setManifestFile( writer.getManifestFile() );
            
            archiver.getArchiver().addDirectory( workDirectory);
            archiver.createArchive( getProject(), archive );

            getProject().getArtifact().setFile( parFile );
        }
        catch ( Exception e )
        {
            throw new MojoExecutionException( "Error assembling PAR", e );
        }

    }
    
 

}
